package com.example.znbk.znbklibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.R;
import com.example.znbk.znbklibrary.widget.ScaleButton;


/**
 * add by wangguolong
 * ---------自定义暂停提示框-----------
 */
public class PauseDialog extends Dialog {


    public PauseDialog(Context context) {
        super(context);
    }

    public PauseDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected PauseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String title;  //标题
        private String message;//提示消息
        private String sbtnText;//消极的
//        private String positive_text;//积极的
        private DialogInterface.OnClickListener sbtnListener;//消极的监听
//        private DialogInterface.OnClickListener rightListener;//积极的监听

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title) {
            if (title == null) {
                this.title = "提醒";
            }
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            if (message == null) {
                this.message = "您没有填写提示信息哦";
            }
            this.message = message;
            return this;
        }

        public Builder setContinueButton(String sbtnText, DialogInterface.OnClickListener sbtnListener) {
            if (sbtnText == null) {
                this.sbtnText = "继续";
            }
            this.sbtnText = sbtnText;
            this.sbtnListener = sbtnListener;

            return this;
        }

//        public Builder setPositionButton(String positive_text, DialogInterface.OnClickListener positiveListener) {
//            if (positive_text == null) {
//                this.positive_text = "确定";
//            }
//            this.positive_text = positive_text;
//            this.rightListener = positiveListener;
//
//            return this;
//        }

        private TextView mTvTitle;  //标题
        private TextView mTvMessage;//提示信息
        private ScaleButton mSBtnContinue;//继续
//        private ScaleButton mSBtnRight;//积极


        public PauseDialog create() {
            final PauseDialog dialog = new PauseDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.layout_pause_dialog, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mTvTitle =  view.findViewById(R.id.tv_title);
            mTvMessage =  view.findViewById(R.id.tv_message);
            mSBtnContinue =  view.findViewById(R.id.sbtn_continue);
//            mSBtnRight =  view.findViewById(R.id.sbtn_right);
            mTvTitle.setText(title);
            mTvMessage.setText(message);
            mSBtnContinue.setText(sbtnText);
//            mSBtnRight.setText(positive_text);
            Window window = dialog.getWindow();
            window.setWindowAnimations(R.style.MyDialogScale);//设置动画效果
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mSBtnContinue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sbtnListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                }
            });

//            mSBtnRight.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    rightListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
//                }
//            });
            dialog.setCanceledOnTouchOutside(false);//禁止点击dialog外部销毁弹窗
            dialog.setOnKeyListener(new OnKeyListener() {//禁止系统返回键值响应
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });
            return dialog;
        }
    }
}